package com.yanqu.road.server.manager.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AllActivityMgr extends TempMgr {

    private static Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();


    public static List<Integer> getAllRunningActivityIds() {
        return new ArrayList<>(ActivityMgr.getActivityMap().keySet());
    }

    public static ActivityInfo getActivityInfo(int activityId) {
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        return activityInfo;
    }

    public static List<ActivityInfo> getUnionRiseRankActivityList(long serverId) {
        List<ActivityInfo> activityList = new ArrayList<>();
        Map<Integer, ActivityInfo> activityMap = ActivityMgr.getActivityMap();
        for (ActivityInfo activityInfo : activityMap.values()) {
            if (activityInfo.getServerIdList().contains(serverId) && activityInTime(activityInfo) && (activityInfo.getType() == eActivityType.CrossUnionActivity.getValue())) {
                activityList.add(activityInfo);
            }
        }
        return activityList;
    }

    private static boolean activityInTime(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return false;
        }
        long now = System.currentTimeMillis() / 1000;
        return now >= activityInfo.getBeginTime() && now <= activityInfo.getEndTime();
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
//        activityInfoMap = ActivityBussiness.getAllRunningActivityInfoMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
